package com.gt.quality.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * 万里悲秋常作客,百年多病独登台
 *
 * @author : makeJava
 */
@Configuration
public class DynamicSchedulingConfig implements SchedulingConfigurer {

    /**
     * 使用虚拟线程优化任务池
     *
     * @return ThreadPoolTaskScheduler
     */
    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        // 设置虚拟线程工厂
        scheduler.setThreadFactory(Thread.ofVirtual().name("vir-scheduler-").factory());
        // 设置核心线程数，根据需求调整
        scheduler.setPoolSize(4);
        scheduler.initialize();
        return scheduler;
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadPoolTaskScheduler taskScheduler = threadPoolTaskScheduler();
        taskRegistrar.setTaskScheduler(taskScheduler);
    }
}

